from django.core.paginator import PageNotAnInteger, Paginator
from django.shortcuts import render
from django.views import View
from .models import UserCourse, UserFavorite, UserMessage
from utils.mixin_utils import LoginRequiredMixin
from organization.models import CourseOrg, Teacher
from courses.models import Course


class MyCourseView(LoginRequiredMixin, View):
    # 我的课程
    def get(self, request):
        user_courses = UserCourse.objects.filter(user=request.user)
        return render(request, 'usercenter-mycourse.html', {
            'user_courses': user_courses,
        })


class MyFavOrgView(LoginRequiredMixin, View):
    # 我的课程-课程学校
    def get(self, request):
        user_id = request.user.id
        org_list = []
        fav_type = UserFavorite.objects.filter(user_id=user_id, fav_type=2)
        for org in fav_type:
            # 取出fav_id也就是机构的id
            org_id = org.fav_id
            # 获取这个机构对象
            org = CourseOrg.objects.filter(id=org_id)
            org_list.append(org)
        return render(request, 'usercenter-fav-org.html', {
            'org_list': org_list,
        })


class MyFavTeacherView(LoginRequiredMixin, View):
    # 授课教师
    def get(self, request):
        teacher_list = []
        fav_teachers = UserFavorite.objects.filter(user=request.user.id, fav_type=3)
        for fav_teacher in fav_teachers:
            teacher_id = fav_teacher.fav_id
            teacher = Teacher.objects.filter(id=teacher_id)
            teacher_list.append(teacher)
        print(teacher_list)
        return render(request, 'usercenter-fav-teacher.html', {
            'teacher_list': teacher_list
        })


class MyFavCourseView(LoginRequiredMixin, View):
    # 公开课程
    def get(self, request):
        course_list = []
        fav_course = UserFavorite.objects.filter(user=request.user.id, fav_type=1)
        for courses in fav_course:
            course_id = courses.fav_id
            course = Course.objects.filter(id=course_id)
            course_list.append(course)
        print(course_list,'哈哈哈啊哈')
        return render(request, 'usercenter-fav-course.html', {
                        'course_list': course_list,
                        })


class MyMessageView(LoginRequiredMixin, View):
    # 我的消息
    def get(self, request):
        all_message = UserMessage.objects.filter(user=request.user.id)
        page = request.GET.get('page', 1)
        p = Paginator(all_message, 1)
        messages = p.page(page)
        return render(request, "usercenter-message.html", {
            "messages": messages,
        })

